﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 参数配置任务
    /// </summary>
    public class ParamConfigTaskService : TaskSchedulingAppService, IParamConfigTaskService
    {
        private IRepository<ParamConfigTask, long> _paramConfigTaskRepository;
        private IRepository<ParamConfig, long> _paramConfigRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="paramConfigTaskRepository"></param>
        /// <param name="paramConfigRepository"></param>
        public ParamConfigTaskService(IRepository<ParamConfigTask, long> paramConfigTaskRepository, IRepository<ParamConfig, long> paramConfigRepository)
        {
            _paramConfigTaskRepository = paramConfigTaskRepository;
            _paramConfigRepository = paramConfigRepository;
        }

        /// <summary>
        /// 获取配置参数记录
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResultDto<ParamConfigTaskDto>> GetListAsync(ParamConfigTaskInput input)
        {
            var iQuery = await _paramConfigTaskRepository.GetQueryableAsync();
            iQuery = iQuery.OrderBy(i => i.OperationTime);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<ParamConfigTask>, List<ParamConfigTaskDto>>(iQuery.ToList());

                return new PagedResultDto<ParamConfigTaskDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Skip((input.Pi - 1) * input.Ps).Take(input.Ps);
                var itemsDto = ObjectMapper.Map<List<ParamConfigTask>, List<ParamConfigTaskDto>>(items.ToList());

                return new PagedResultDto<ParamConfigTaskDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 创建配置参数任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ParamConfigTaskDto> PostAsync(ParamConfigTaskCreateDto input)
        {
            var paramConfig = await _paramConfigRepository.GetAsync(p => p.ParamCodg == input.ParamCodg);

            if (string.IsNullOrEmpty(paramConfig.PropertyKey))
            { 
                throw new UserFriendlyException(message: "配置参数属性Key为空");
            }

            var paramConfigTask = ObjectMapper.Map<ParamConfigTaskCreateDto, ParamConfigTask>(input);
            paramConfigTask.SetPropertyKey(paramConfig.PropertyKey);
            paramConfigTask.TaskContdesc = $"配置参数属性Key:{paramConfig.PropertyKey},值：{input.PropertyValue}";
            paramConfigTask = await _paramConfigTaskRepository.InsertAsync(paramConfigTask);

            var paramConfigTaskDto = ObjectMapper.Map<ParamConfigTask, ParamConfigTaskDto>(paramConfigTask);
            RedisHelper.SetString(paramConfig.PropertyKey, input.PropertyValue);
            

            return paramConfigTaskDto;
        }
    }
}
